﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.Core.MiniApi.Attributes
 * 唯一标识：a264e5cc-aaa0-4d8b-b669-67d3307b4e72
 * 文件名：EndpointFilterAttribute
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2023/12/20 15:51:08
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

namespace Vampirewal.Core.MiniApi;

public sealed class VampirewalMiniApiResultProblemDetailsWriter : IProblemDetailsWriter
{
    public bool CanWrite(ProblemDetailsContext context)
    {
        // 获取是否允许过滤
        return context.HttpContext.Response.StatusCode != 200;
    }

    public async ValueTask WriteAsync(ProblemDetailsContext context)
    {
        var param = context.ProblemDetails;

        ApiResult result = new()
        {
            Code = param.Status,
            IsSuccess = param.Status == 200
        };
        result.Message = param.Detail;
        await context.HttpContext.Response.WriteAsJsonAsync(result);
    }
}